package com.chengshi.shop.service.wechat.impl;

import com.chengshi.shop.dao.member.MemberMapper;
import com.chengshi.shop.model.member.Member;
import com.chengshi.shop.model.order.Order;
import com.chengshi.shop.service.wechat.MessageService;
import com.chengshi.shop.util.ConfigureUtil;
import com.chengshi.shop.util.DateFormatUtil;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yuanrs
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private WxMpService wxMpService;
    @Resource
    private MemberMapper memberMapper;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 到账通知发送消息
     *
     * @param message
     * @param url
     * @param member
     */
    @Override
    public void arrivalNoticeMsg(String message, String url, Member member) {
        List<WxMpTemplateData> dataList = new ArrayList<>();
        WxMpTemplateData data = new WxMpTemplateData();
        data.setName("first");
        data.setValue(message);
        data.setColor("#ff0000");
        dataList.add(data);
        WxMpTemplateData data1 = new WxMpTemplateData();
        data1.setName("keyword1");
        data1.setValue(member.getMobile());
        data1.setColor("#173177");
        dataList.add(data1);
        WxMpTemplateData data2 = new WxMpTemplateData();
        data2.setName("keyword2");
        data2.setValue("详见账户");
        data2.setColor("#173177");
        dataList.add(data2);
        WxMpTemplateData data3 = new WxMpTemplateData();
        data3.setName("keyword3");
        data3.setValue(DateFormatUtil.formatDate(new Date(), DateFormatUtil.FULL_DATE_FORMAT));
        data3.setColor("#173177");
        dataList.add(data3);
        WxMpTemplateData data4 = new WxMpTemplateData();
        data4.setName("remark");
        data4.setValue("点击进入个人中心查看，如有疑问请联系客服");
        data4.setColor("#3385ff");
        dataList.add(data4);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(
                    WxMpTemplateMessage.builder()
                            .toUser(member.getOpenId())
                            .templateId(ConfigureUtil.config("arrivalNoticeId"))
                            .data(dataList)
                            .url(url).build());
        } catch (WxErrorException e) {
            e.printStackTrace();
            logger.error("模板消息发送失败{}", e.getError());
        }
    }

    /**
     * 订单发货通知
     *
     * @param order
     * @param deliveryGoods
     * @param openId
     */
    @Override
    public void orderDeliveryMsg(Order order, String deliveryGoods, String openId) {
        List<WxMpTemplateData> dataList = new ArrayList<>();
        WxMpTemplateData data = new WxMpTemplateData();
        data.setName("first");
        data.setValue("您好，您的订单" + order.getOrderNum() + "已开始发货啦，正快马加鞭向您飞奔而去");
        data.setColor("#ff0000");
        dataList.add(data);
        WxMpTemplateData data1 = new WxMpTemplateData();
        data1.setName("keyword1");
        data1.setValue(deliveryGoods);
        data1.setColor("#173177");
        dataList.add(data1);
        WxMpTemplateData data2 = new WxMpTemplateData();
        data2.setName("keyword2");
        data2.setValue(DateFormatUtil.formatDate(order.getDeliveryTime(), DateFormatUtil.FULL_DATE_FORMAT));
        data2.setColor("#173177");
        dataList.add(data2);
        WxMpTemplateData data3 = new WxMpTemplateData();
        data3.setName("keyword3");
        data3.setValue(order.getExpressName());
        data3.setColor("#173177");
        dataList.add(data3);
        WxMpTemplateData data4 = new WxMpTemplateData();
        data4.setName("keyword4");
        data4.setValue(order.getExpressNum());
        data4.setColor("#173177");
        dataList.add(data4);
        WxMpTemplateData data5 = new WxMpTemplateData();
        data5.setName("keyword5");
        data5.setValue(order.getAddress());
        data5.setColor("#173177");
        dataList.add(data5);
        WxMpTemplateData data6 = new WxMpTemplateData();
        data6.setName("remark");
        data6.setValue("点击进入查看订单详情，如有疑问请联系客服");
        data6.setColor("#3385ff");
        dataList.add(data6);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(
                    WxMpTemplateMessage.builder()
                            .toUser(openId)
                            .templateId(ConfigureUtil.config("wechat.template.orderDelivery"))
                            .data(dataList)
                            .url(ConfigureUtil.config("wechat.mp.host") + "/orderDetail/" + order.getOrderId()).build());
        } catch (WxErrorException e) {
            e.printStackTrace();
            logger.error("模板消息发送失败{}", e.getError());
        }
    }

    /**
     * 订单关闭通知
     *
     * @param order
     * @param message
     * @param openId
     */
    @Override
    public void orderCloseMsg(Order order, String message, String openId) {
        List<WxMpTemplateData> dataList = new ArrayList<>();
        WxMpTemplateData data = new WxMpTemplateData();
        data.setName("first");
        data.setValue("您好，您有一笔订单已经交易关闭");
        data.setColor("#ff0000");
        dataList.add(data);
        WxMpTemplateData data1 = new WxMpTemplateData();
        data1.setName("keyword1");
        data1.setValue(order.getOrderNum());
        data1.setColor("#173177");
        dataList.add(data1);
        WxMpTemplateData data2 = new WxMpTemplateData();
        data2.setName("keyword2");
        data2.setValue(DateFormatUtil.formatDate(order.getCreateTime(), DateFormatUtil.FULL_DATE_FORMAT));
        data2.setColor("#173177");
        dataList.add(data2);
        WxMpTemplateData data3 = new WxMpTemplateData();
        data3.setName("keyword3");
        data3.setValue(message);
        data3.setColor("#173177");
        dataList.add(data3);
        WxMpTemplateData data4 = new WxMpTemplateData();
        data4.setName("keyword4");
        data4.setValue(DateFormatUtil.formatDate(new Date(), DateFormatUtil.FULL_DATE_FORMAT));
        data4.setColor("#173177");
        dataList.add(data4);
        WxMpTemplateData data5 = new WxMpTemplateData();
        data5.setName("remark");
        data5.setValue("点击进入查看订单详情，如有疑问请联系客服");
        data5.setColor("#3385ff");
        dataList.add(data5);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(
                    WxMpTemplateMessage.builder()
                            .toUser(openId)
                            .templateId(ConfigureUtil.config("wechat.template.orderClose"))
                            .data(dataList)
                            .url(ConfigureUtil.config("wechat.mp.host") + "/orderDetail/" + order.getOrderId()).build());
        } catch (WxErrorException e) {
            e.printStackTrace();
            logger.error("模板消息发送失败{}", e.getError());
        }
    }

    /**
     * 订单自动确认收货
     *
     * @param order
     * @param deliveryGoods
     * @param openId
     */
    @Override
    public void orderReceiptMsg(Order order, String deliveryGoods, String openId) {
        List<WxMpTemplateData> dataList = new ArrayList<>();
        WxMpTemplateData data = new WxMpTemplateData();
        data.setName("first");
        data.setValue("您好，您有一笔订单已经确认收货");
        data.setColor("#ff0000");
        dataList.add(data);
        WxMpTemplateData data1 = new WxMpTemplateData();
        data1.setName("keyword1");
        data1.setValue(order.getOrderNum());
        data1.setColor("#173177");
        dataList.add(data1);
        WxMpTemplateData data2 = new WxMpTemplateData();
        data2.setName("keyword2");
        data2.setValue(deliveryGoods);
        data2.setColor("#173177");
        dataList.add(data2);
        WxMpTemplateData data3 = new WxMpTemplateData();
        data3.setName("keyword3");
        data3.setValue(DateFormatUtil.formatDate(order.getCreateTime(), DateFormatUtil.FULL_DATE_FORMAT));
        data3.setColor("#173177");
        dataList.add(data3);
        WxMpTemplateData data4 = new WxMpTemplateData();
        data4.setName("keyword4");
        data4.setValue(DateFormatUtil.formatDate(order.getDeliveryTime(), DateFormatUtil.FULL_DATE_FORMAT));
        data4.setColor("#173177");
        dataList.add(data4);
        WxMpTemplateData data5 = new WxMpTemplateData();
        data5.setName("keyword5");
        data5.setValue(DateFormatUtil.formatDate(order.getFinishTime(), DateFormatUtil.FULL_DATE_FORMAT));
        data5.setColor("#173177");
        dataList.add(data5);
        WxMpTemplateData data6 = new WxMpTemplateData();
        data6.setName("remark");
        data6.setValue("点击进入查看订单详情，如有疑问请联系客服");
        data6.setColor("#3385ff");
        dataList.add(data6);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(
                    WxMpTemplateMessage.builder()
                            .toUser(openId)
                            .templateId(ConfigureUtil.config("wechat.template.orderReceipt"))
                            .data(dataList)
                            .url(ConfigureUtil.config("wechat.mp.host") + "/orderDetail/" + order.getOrderId()).build());
        } catch (WxErrorException e) {
            e.printStackTrace();
            logger.error("模板消息发送失败{}", e.getError());
        }
    }

    /**
     * 通知管理员发货
     *
     * @param num
     */
    @Override
    public void sendDeliveryNotice(Integer num) {
        List<Member> memberList = memberMapper.getAdminList();
        for (Member member : memberList){
            List<WxMpTemplateData> dataList = new ArrayList<>();
            WxMpTemplateData data = new WxMpTemplateData();
            data.setName("first");
            data.setValue("您好，您还有待发货的订单，请注意及时发货");
            data.setColor("#ff0000");
            dataList.add(data);
            WxMpTemplateData data1 = new WxMpTemplateData();
            data1.setName("keyword1");
            data1.setValue(num + "笔");
            data1.setColor("#173177");
            dataList.add(data1);
            WxMpTemplateData data2 = new WxMpTemplateData();
            data2.setName("keyword2");
            data2.setValue(DateFormatUtil.formatDate(new Date(), DateFormatUtil.FULL_DATE_FORMAT));
            data2.setColor("#173177");
            dataList.add(data2);
            WxMpTemplateData data3 = new WxMpTemplateData();
            data3.setName("remark");
            data3.setValue("点击进入订单管理后台查看，如有疑问请联系管理员");
            data3.setColor("#3385ff");
            dataList.add(data3);
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(
                        WxMpTemplateMessage.builder()
                                .toUser(member.getOpenId())
                                .templateId(ConfigureUtil.config("wechat.template.waitDelivery"))
                                .data(dataList)
                                .url(ConfigureUtil.config("wechat.mp.host")+"/manage/home").build());
            } catch (WxErrorException e) {
                e.printStackTrace();
                logger.error("模板消息发送失败{}", e.getError());
            }
        }
    }
}